var CONDITION_NUM=1
var FEATURE_NUM=1
var FEATURE_LIST=[]
layui.use(['form','table'], function(){
    var form = layui.form
    ,layer = layui.layer 
    ,table=layui.table
    
    //监听提交
    form.on('submit(demo1)', function(data){
      layer.alert(JSON.stringify(data.field), {
        title: '最终的提交信息'
      })
      return false;
    });
   
    renderConditions()
    form.on('submit(feature_sumbit)', function (data) {
      
      var feature_names=[]
      for(key in data.field){
        if(key.indexOf("feature_name")>-1&&data.field[key].length>0){
          feature_names.push(data.field[key])
        }
      }
      if(data.field['conclusionn'].length<1){
        layer.alert("结论不能为空")
        return false
      }
      console.info(feature_names)
      var reqData={
          feature_names:feature_names,
          conclusionn:data.field['conclusionn'],
          is_temp:data.field['is_temp']
        
      }
      if(feature_names.length<1){
        layer.alert("条件特征不能为空")
        return false
      }
      $.ajax({ 
				url:'/add-rule',   
				method:'post',
        async:false,   
				data:JSON.stringify(reqData),    
				dataType:'JSON',    
				success:function(res){   
					if(res.status=='ok'){  
            $("#add_rule_form")[0].reset()
					  layer.msg("提交成功",{icon: 1, time: 3000},function(){location.reload();}); //提交成功后刷新页面
          }        
					else  alert(res.msg); 
          return false
					},       
					error:function (data) {
					}     
			});
      return false;
    })

    //渲染数据表格

    table.render({
      elem: '#rules_table'
      ,url: '/show-rules'
      ,cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
      ,cols: [[
        {field:'id',  title: 'id',hide:true},
        {field:'rule_id', width:100, title: 'ID', sort: true}
        ,{field:'conds',  title: '条件'}
        ,{field:'result',  title: '结论'}
        ,{field:'is_temp_desc', width:100,title: '最终结论', sort: true}
      ]],
    });

  });


function renderConditions(){
    
    $.ajax({
      url:'/get-feature',   
      method:'get',
      async:false,   
      dataType:'JSON',    
      success:function(res){ 
        FEATURE_LIST=res
        for(var index=0;index<res.length;index++){
          $("#conditions_select").append("<option value="+res[index].id+">"+res[index].name+"</option>")
          if(res[index].is_pstv==0){
            $("#target_select").append("<option value="+res[index].id+">"+res[index].name+"</option>")
          }
          
        }
      },       
      error:function (data) {
      }     
    });

    layui.form.render("select")
    
}

var __tmpt="<div class=\"layui-form-item\">"+
"<div class=\"layui-inline\">"+
  "<label class=\"layui-form-label\">推理条件</label>"+
  "<div class=\"layui-input-inline\">"+
    "<select name=\"conditions_select\" id=\"conditions_select\"  lay-verify=\"required\" lay-search=\"\">"+
      "<option value=\"\">直接选择或搜索选择</option>"+
    "</select>"+
  "</div>"+
  "<button type=\"button\" onclick=\"del_condition(this)\" "+
  "class=\"layui-btn layui-btn-danger layui-btn-sm\">"+
  "<i class=\"layui-icon layui-icon-delete\"></i>"+
    "</button>"+
"</div>"+
"</div>"
function add_condition(){
  CONDITION_NUM++
    var rct=__tmpt.replaceAll("conditions_select","conditions_select_"+CONDITION_NUM)
    rct=rct.replaceAll("推理条件","推理条件"+CONDITION_NUM)
    $("#first_condition_node").append(rct)
    
    for(var index=0;index<FEATURE_LIST.length;index++){
      $("#conditions_select_"+CONDITION_NUM).append("<option value="+FEATURE_LIST[index].id+">"+FEATURE_LIST[index].name+"</option>")
    }
    layui.form.render("select")
}


function del_condition(_this){
    $(_this).parent().parent().remove()
    //CONDITION_NUM--
}


var __feature_tmpt="<div class=\"layui-form-item\" id=\"feature_name\">"+
"<div class=\"layui-inline\">"+
  "<label class=\"layui-form-label\">特征 ：</label>"+
  "<div class=\"layui-input-inline\">"+
    "<input type=\"text\" name=\"feature_name\" class=\"layui-input\">"+
  "</div>"+
  "<button type=\"button\" onclick=\"del_condition(this)\" "+
  "class=\"layui-btn layui-btn-danger layui-btn-sm\">"+
  "<i class=\"layui-icon layui-icon-delete\"></i>"+
    "</button>"+
"</div>"+
"</div>"
function add_feature(){
  FEATURE_NUM++
  var rct=__feature_tmpt.replaceAll("feature_name","feature_name_"+(FEATURE_NUM))
  rct=rct.replaceAll("特征","特征-"+FEATURE_NUM)
  $("#first_feature_node").append(rct)
}




